<?
//文件操作类
	//生成文件、删除文件均只能在本站点内操作
class ActionFile
{
	var $Root;
	function __construct()
	{
	 	$this->Root=RootDir;

	}
	///创建文件夹 在已有文件夹下
	function CreateFolder($dir)
	{
		$dir=$this->Root.$dir;

		if (!file_exists($dir))
		 {
			mkdir($dir, 0777);
			return true;
		 }
		 return false;
	}

	//删除某一个具体的文件 路径以根目录为准的相对路径
	function DelFile($file)
	{
		if(!unlink($this->Root.$file))
		{
			return false;
		}
		return true;
	}

	//创建文件 $sFile文件名称,$cotent文件内容
	function CreatFile($sFile,$content="")
	{


			$fp= fopen($this->Root.$sFile,"w+");
			if(!fwrite($fp,$content))
			{
				echo "文件操作失败";
				return false;
			}
			fclose($fp);
 	   return true;
	}

	//删除文件夹和其下的目录及文件
	function DelDir($dir="")
	 {
	 	$dir=$this->Root.$dir;
		if($dir=="") return false;
		$dh=opendir($dir);
		 while ($file=readdir($dh))
		 {//readdir函数返回目录中下一个文件的文件名。文件名以在文件系统中的排序返回

			if($file!="." && $file!="..")
			 {
				 $fullpath=$dir."/".$file;
				if(!is_dir($fullpath))
				{//is_dir函数：如果文件名存在并且为目录则返回 TRUE
				   unlink($fullpath);//unlink函数删除文件
				}
				else
				{
				 $this->DelDir($fullpath);
				}
			}
		 }

	   closedir($dh);

	   if(rmdir($dir))
	   {//rmdir函数删除目录
		 return true;
	   }
	   else
	   {
		 return false;
	   }
	}

	/**
	 * 生成任意Url地址为文件
	 * @param str_url $anyPagUrl  任意可访问的url地址
	 * @param str $toPagName 欲生成的文件名，传完整的文件名
	 * @return 成功返回true 失败返回false
	 */
	 function CreateAnyPageToPage($anyPagUrl,$toPagName)
	{
				//$_SERVER['HTTP_HOST'];//得到当前网址
			//要想更改欲生成的网页，只需把$pagurl改了就行
			if(strpos($toPagName,"/")>=0)
			{
				$path=dirname($toPagName);
			}
			else
			{
				$path="";
			}


			$content=@file($anyPagUrl) or die("Failed to open the url ".$anyPagUrl." !");
			$content=@join("",$content);

			if($path != "")
			{
				if(!file_exists($path))
				{
					 $this->CreateFolder($path);
				 }
			}
			if ($toPagName<>"")

					$fp=@fopen($toPagName,"w+") or die("Failed to open the file ".$toPagName." !");
					//$content=preg_replace("/PHPSESSID\=[w]{5,}/",$content);
					 if( @fwrite($fp,$content) )

			   {
				@fclose($fp);
				return true;
			   }
					 else
					 {
				@fclose($fp);
					  return false;
					 }

	 }


	//列出目录下的所有被允许的文件类型
	//$allowType为空时表示返回目录下所有的文件及文件夹   返回array
	function   listDirTree($dirName  = NULL,$allowType)
	{
		 $dirName=$this->ROOT.$dirName;
		 if(!is_dir($dirName))
			exit("This is't any path.");
		  $tree=array();
			 if ($handle = opendir($dirName ))
			 {
				while (false !== ($file = readdir($handle)))
				{
					if($allowType!="")
					{
						if($file!="." and $file!=".." and  !strpos($allowType,end(explode(".",$file)))==false)
					{
							$tree[]=$file;
					}
					}
					else
					{
						if($file!="." and $file!="..")
						{
							$tree[]=$file;
						}
					}

				}
				closedir($handle);
			}

		return $tree;
	}


	//拷贝源文件夹到目标路径下 返加bool
	function xCopy($from_dir, $to_dir, $child)
	{
		//用法：
		// xCopy("feiy","feiy2",1):拷贝feiy下的文件到 feiy2,包括子目录
		// xCopy("feiy","feiy2",0):拷贝feiy下的文件到 feiy2,不包括子目录
	 	$from_dir= $this->Root.$from_dir;
	 	$to_dir=$this->Root.$to_dir;
		if(!is_dir($from_dir)){
		echo("Error:the $source is not a direction!");
		return false;
		}
		if(!is_dir($to_dir)){
		mkdir($to_dir,0777);
		}
		$handle=dir($from_dir);
		while($entry=$handle->read()) {
			if(($entry!=".")&&($entry!="..")){
				if(is_dir($from_dir."/".$entry)){
					if($child)    $this->xCopy($from_dir."/".$entry,$to_dir."/".$entry,$child);
				}else{
					copy($from_dir."/".$entry,$to_dir."/".$entry);
				}
			}
		}
		return true;
	}

	//播放swf
	function PlaySWF($SWFUrl,$sWidth,$sheight)
	{
		echo "<embed src='".$SWFUrl."' width='".$sWidth."' height='".$sheight."' wmode='transparent' type='application/x-shockwave-flash'>";
	}

	//播放视频代码
	function PlayVedio($url,$sWidth,$shenght)
	{
		print "
		<OBJECT id='NSPlay' codeBase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 type=application/x-oleobject height=".$shenght." standby='Loading Microsoft? Windows Media? Player components...' width=".$sWidth." classid=CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6>
<PARAM NAME='url' VALUE='".$url."'>
<PARAM NAME='rate' VALUE='1'>
<PARAM NAME='balance' VALUE='0'>
<PARAM NAME='playCount' VALUE='1'>
<PARAM NAME='autoStart' VALUE='-1'>
<PARAM NAME='currentMarker' VALUE='0'>
<PARAM NAME='invokeURLs' VALUE='-1'>
<PARAM NAME='baseURL' VALUE=''>
<PARAM NAME='volume' VALUE='100'>
<PARAM NAME='mute' VALUE='0'>
<PARAM NAME='uiMode' VALUE='full'>
<PARAM NAME='stretchToFit' VALUE='0'>
<PARAM NAME='windowlessVideo' VALUE='0'>
<PARAM NAME='enabled' VALUE='-1'>
<PARAM NAME='enableContextMenu' VALUE='-1'>
<PARAM NAME='fullScreen' VALUE='0'>
<PARAM NAME='enableErrorDialogs' VALUE='0'>
</OBJECT>";
	}

	//读取文件内容
	//当$type=1时为读取本地文件内容 为2时，读取任意url地址的html代码
	function readFileContent($sFileName,$type="1")
	{
		if($type=="1" and is_file($sFileName))
		{
			return file_get_contents($sFileName);
		}
		elseif($type=="2")
		{
			$content=@file($sFileName);
			if($content)
			{
				$content=@join("",$content);
				return $content;
			}
			else
			{
				return null;
			}
		}
		else
		{
			return null;
		}
	}

	//当写入数据库时转换欲写入的内容的特殊字符
	function turnWrite($strContent="")
	{
		$content = stripslashes($strContent);
		$content = eregi_replace("##textarea","<textarea",$content);
		$content = eregi_replace("##/textarea","</textarea",$content);
		$content = eregi_replace("##form","<form",$content);
		$content = eregi_replace("##/form","</form",$content);
		return $content;
	}

	//当在输入框时显示输入框时转换格式
	function turnRead($strContent="")
	{
		$content = eregi_replace("<textarea","##textarea",$strContent);
		$content = eregi_replace("</textarea","##/textarea",$content);
		$content = eregi_replace("<form","##form",$content);
		$content = eregi_replace("</form","##/form",$content);
		return $content;
	}

}
?>